﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MemoryManagement
{
    class BufferedConsole : IDisposable
    {
        private const int MAX_BUFFER_SIZE = 25;
        private StringBuilder buffer;

        public BufferedConsole()
        {
            buffer = new StringBuilder();
        }

        public void Write(string str)
        {
            buffer.Append(str);
            if (buffer.Length >= MAX_BUFFER_SIZE)
            {
                Console.Write(buffer.ToString());
            }
        }

        public void WriteLine(string str)
        {
            buffer.Append(str).Append(Environment.NewLine);
            if (buffer.Length >= MAX_BUFFER_SIZE)
            {
                Console.WriteLine(buffer.ToString());
            }
        }

        ~BufferedConsole()
        {
            Dispose(false);
        }
        
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Free any managed resources here
            }

            Console.WriteLine(buffer.ToString());
            buffer = new StringBuilder();
        }
    }
}
